package com.ddone.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ddone
 * @date 2024/3/23-20:24
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private ServletContext servletContext;

    @PostMapping("/regist")
    public String regist(String account, @RequestParam("header") MultipartFile header, Model model) throws IOException {
        // 转出处理
        /*
         * Todo: 注册对象处理
         *  1. 将文件转存到其他位置
         *      本地磁盘
         *      其他文件服务器
         *      第三方文件服务器
         *  2. 将文件存储路径, 存储到用户的数据库表中
         */
        String basePath = this.servletContext.getRealPath("/image");
        File fileDic = new File(basePath);
        if (!fileDic.exists())
            fileDic.mkdirs();
        String filename = header.getOriginalFilename();
        String pre = UUID.randomUUID().toString().replaceAll("-", "");
        filename = pre + filename;
        File imgFile = new File(fileDic, filename);
        header.transferTo(imgFile);
        model.addAttribute("src", "/image/" + filename);
        return "upload";
    }

    @GetMapping({"download"})
    public void downLoad(HttpServletResponse response) throws IOException {
        // 1. 读取服务器中文件
        String realPath = this.servletContext.getRealPath("/image/zls.png");
        FileInputStream fis = new FileInputStream(realPath);
        // 2. 设置下载响应头
        response.setHeader("content-disposition", "attachment;filename=zls.png");
        ServletOutputStream ops = response.getOutputStream();
        // 3. 字节输出流协会客户端
        byte[] buffer = new byte[8192];
        int len = -1;
        while ((len = fis.read(buffer)) != -1)
            ops.write(buffer, 0, len);
        ops.close();
        fis.close();
    }

    /**
     *
     * String: 逻辑视图 转发 重定向
     * JSON： Map  Bean List  @ResponseBody
     * void: 自己利用响应对象处理返回数据  转发 重定向 json 返回字节文件
     * ResponseEntity<T>:直接写回数据 类型 T 任何类型 对void的优化
     */

    @GetMapping({"download-mvc"})
    public ResponseEntity<byte[]> downLoad() throws IOException {
        //1.读取服务器中文件 【FileInputStream】
        String realPath = this.servletContext.getRealPath("/image/zls.png");
        FileInputStream fis = new FileInputStream(realPath);
        int lenght = fis.available();
        byte[] data = new byte[lenght];
        fis.read(data);
        //ByteArrayOutputStream
        //2.设置下载响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add("content-disposition", "attachment;filename=zls.png");
        //3.封装ResponseEntity对象
        ResponseEntity<byte[]> entity = new ResponseEntity(data, (MultiValueMap)headers, HttpStatus.OK);
        return entity;
    }
}